package com.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.core.dao.Online_accountDAO;
import com.core.database.Manager;
import com.core.entities.Online_account;
import com.core.exception.DAOException;

public class Online_accountDAOImpl implements Online_accountDAO {
	Online_account online_account = new Online_account();
	Connection connection = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	ArrayList<Online_account> online_accountList = new ArrayList<Online_account>();

	private static Online_accountDAOImpl instance = null;

	public static Online_accountDAOImpl getInstance() {
		if (instance == null) {
			instance = new Online_accountDAOImpl();
		}
		return instance;
	}

	@Override
	public void insert(Online_account o) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "INSERT INTO online_account (facebook, twitter, google) VALUE (?, ?, ?)";
			ps = connection.prepareStatement(sql);
			ps.setString(1, o.getFacebook());
			ps.setString(2, o.getTwitter());
			ps.setString(3, o.getGoogle());

			int result = ps.executeUpdate();
			if (result != 1) {
				throw new SQLException(
						"No row inserted in Online_account table!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("cannot add new Online_account", ex);
		} finally {
			Manager.closeConnection(connection);
		}

	}

	@Override
	public Online_account findById(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM online_account WHERE id = '" + id + "'";
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					online_account.setId(rs.getInt(1));
					online_account.setFacebook(rs.getString(2));
					online_account.setTwitter(rs.getString(3));
					online_account.setGoogle(rs.getString(4));

				}
			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return online_account;
	}

	@Override
	public List<Online_account> findAll() throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM online_account ";
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					online_account.setId(rs.getInt(1));
					online_account.setFacebook(rs.getString(2));
					online_account.setTwitter(rs.getString(3));
					online_account.setGoogle(rs.getString(4));
					online_accountList.add(online_account);

				}
			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return online_accountList;
	}

	@Override
	public void delete(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " DELETE FROM online_account WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate();
			if (result != 1) {
				ps.setString(1, online_account.getFacebook());
				ps.setString(2, online_account.getTwitter());
				ps.setString(3, online_account.getGoogle());

			}

		} catch (SQLException s) {
			s.printStackTrace();

		}

	}

	@Override
	public void update(Online_account o) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "UPDATE online_account SET facebook'"
					+ o.getFacebook() + "', twitter = '" + o.getTwitter()
					+ "', google ='" + o.getGoogle() + "' WHERE id = "
					+ o.getId();
			ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate(sql);

			if (result != 1) {
				ps.setString(1, online_account.getFacebook());
				ps.setString(2, online_account.getTwitter());
				ps.setString(3, online_account.getGoogle());
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}

	}

	@Override
	public Online_account findByFacebook(String facebook) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM online_account WHERE facebook = '"
					+ facebook + "'";
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					online_account.setId(rs.getInt(1));
					online_account.setFacebook(rs.getString(2));
					online_account.setTwitter(rs.getString(3));
					online_account.setGoogle(rs.getString(4));

				}
			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return online_account;
	}

	@Override
	public Online_account findByTwitter(String twitter) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM online_account WHERE twitter = '"
					+ twitter + "'";
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					online_account.setId(rs.getInt(1));
					online_account.setFacebook(rs.getString(2));
					online_account.setTwitter(rs.getString(3));
					online_account.setGoogle(rs.getString(4));

				}
			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return online_account;
	}

	@Override
	public Online_account findByGoogle(String google) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM online_account WHERE google = '"
					+ google + "'";
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					online_account.setId(rs.getInt(1));
					online_account.setFacebook(rs.getString(2));
					online_account.setTwitter(rs.getString(3));
					online_account.setGoogle(rs.getString(4));

				}
			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return online_account;
	}

}
